<?php

namespace app\admin\controller\union;
/* 用户管理 */

use app\admin\library\Auth;
use app\admin\model\Admin;
use app\admin\model\union\Guild;
use app\common\controller\Backend;
use app\admin\model\union\GuildHostManage;
use fast\AliyunUtil;

class Guildhost extends Backend
{
    protected $model = null;
    protected $searchFields = '';
    protected $admin_id = null;
    protected $guild_id = 0;
    protected $is_view = 0;
    protected $guild_manage_id = 0;
    protected $base_url = 'https://www.yyapp.love/guild/bind.html';
    public function _initialize()
    {
        parent::_initialize();
        //获取后台登录管理员id
        $auth = Auth::instance();
        $this->admin_id = $auth->isLogin() ? $auth->id : 0;
        $this->assign('admin_id', $this->admin_id);
        $adminInfo = Admin::get(intval($auth->id));
        $this->guild_id = $adminInfo['guild_id'];
        $agent_app = $adminInfo['agent_app']; //代理APP地址
        if($agent_app) $this->base_url = $agent_app; //如果代理有选择代理地址，则重置
//        if($this->guild_id)$this->guild_manage_id = Guild::where('guild_id',$this->guild_id)->value('id');
        $guild = Guild::where('id',$this->guild_id)->value('guild_id');
        $this->base_url = $this->base_url.'?guild_id='.$guild;
        $this->assign('share_url', $this->base_url);
        $this->model = new GuildHostManage();

    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        $child_data = Guild::getChildGuild($this->guild_id);
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $filter = json_decode($this->request->get('filter'),true);
            $op = json_decode($this->request->get('op'),true);
            if (isset($op['guild_name'])){
                if ($op['guild_name']) $op['g.guild_manage_id'] = $op['guild_name'];
                $filter['g.guild_manage_id'] = $filter['guild_name'];
                unset($op['guild_name']);
                unset($filter['guild_name']);
                $this->request->get(['op'=>json_encode($op)]);
                $this->request->get(['filter'=>json_encode($filter)]);
            }
            if (isset($op['user_status'])){ //用户状态
//               print_r($filter);
                if ($op['user_status']) $op['uu.status'] = $op['user_status'];
                $filter['uu.status'] = $filter['user_status'];
                unset($op['user_status']);
                unset($filter['user_status']);
                $this->request->get(['op'=>json_encode($op)]);
                $this->request->get(['filter'=>json_encode($filter)]);
            }
            if (isset($op['role'])){ //认证状态
                if ($op['role']) $op['u.role'] = $op['role'];
                $filter['u.role'] = $filter['role'];
                unset($op['role']);
                unset($filter['role']);
                $this->request->get(['op'=>json_encode($op)]);
                $this->request->get(['filter'=>json_encode($filter)]);
            }
            if (isset($op['status'])){ //签约状态
//               print_r($filter);
                if ($op['status']) $op['g.status'] = $op['status'];
                $filter['g.status'] = $filter['status'];
                unset($op['status']);
                unset($filter['status']);
                $this->request->get(['op'=>json_encode($op)]);
                $this->request->get(['filter'=>json_encode($filter)]);
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();


            //状态：0-有效，1-冻结，2-注销
            $field = "u.user_id,u.user_profile_id,u.role,u.online,u.nick_name,u.avatar,u.charm_value,u.role,uu.status as user_status,g.* ";
            $list = $this->model->field($field)
                ->alias('g')
                ->join('zy_voice_call.t_user_profile u',"u.user_id=g.host_user_id","LEFT")
                ->join('zy_voice_call.t_user uu',"u.user_id = uu.id","LEFT")
                ->where($where);
//            if ($this->guild_id){
//                $where2['guild_manage_id'] = $this->guild_id;
//                $list = $list->where($where2);
//            }
            if (!empty($child_data)){
                $child_id = array_keys($child_data);
                array_push($child_id,$this->guild_id);
                $list = $list->where('guild_manage_id','in',$child_id);
            }else{
                $where2['guild_manage_id'] = $this->guild_id;
                $list = $list->where($where2);
            }
            $list = $list->order($sort, $order)
                ->paginate($limit);

            //超推名称
            $guild_row = Guild::field('id,name')->where('id',$this->guild_id)->find();
            if ($guild_row)$guild_row = $guild_row->toArray();
            foreach ($list->items() as $v){
                if($v->avatar)$v->avatar= AliyunUtil::buidResUrl($v->avatar,'img');
                if ($this->guild_id == $v->guild_manage_id){
                    $v->guild_name = $guild_row['name'];
                }else if (!empty($child_data[$v->guild_manage_id])){
                    if ($this->guild_id == 0){
                        $v->guild_name = $child_data[$v->guild_manage_id];
                    }else{
                        $v->guild_name = $guild_row['name'].'-'.$child_data[$v->guild_manage_id];
                    }
                }else{
                    $v->guild_name = '';
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        $child_data[$this->guild_id] = '自己';
        $this->view->assign('union_list',$child_data);
        return $this->view->fetch();
    }
    /**
     * 移除用户
     */
    public function unbind(){
        $id = input('ids');
        $record = GuildHostManage::get($id);
        if (!$record) return ['ret' => -1, 'message' => '记录不存在'];
        //没有权限删除下级超推用户
        if ($record->guild_manage_id !== $this->guild_id){
            return ['ret' => -1, 'msg' => '没有权限'];
        }
        $this->model->save([
            'status'  => 0,
            'unbind_at' => date('Y-m-d H:i:s')
        ],['id' => $id]);
        return ['ret' => 0, 'msg' => '操作成功！'];
    }
}